package com.ruoyi.rsikManage.task;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.rsikManage.domain.TRiskOrgidUnitcode;
import com.ruoyi.rsikManage.service.ITRiskOrgidUnitcodeService;
import com.ruoyi.rsikManage.service.JyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class OrgInfoUpdateScheduler {

    @Autowired
    JyService jyService;
    @Value("${jy.switch}")
    private boolean JY_SWITCH;

    @Autowired
    ITRiskOrgidUnitcodeService tRiskOrgidUnitcodeService;

//    @Scheduled(cron = "*/1 * * * * *") // 每5秒执行一次
    public void OrgInfoUpdate()  {

       if (!JY_SWITCH){
//           String orgName = jyService.getIndexAndName();

           String orgName ="{\n" +
                   "    \"error_code\": \"0000000000\",\n" +
                   "    \"message\": \"操作成功\",\n" +
                   "    \"data\": [\n" +
                   "        {\n" +
                   "            \"org_id\": 2,\n" +
                   "            \"parent_id\": 0,\n" +
                   "            \"org_index\": \"10\",\n" +
                   "            \"org_parent_index\": \"0\",\n" +
                   "            \"org_name\": \"本域\",\n" +
                   "            \"region_code\": \"100000\",\n" +
                   "            \"region_code_desc\": null,\n" +
                   "            \"cascaded_id\": null,\n" +
                   "            \"address\": null,\n" +
                   "            \"description\": null,\n" +
                   "            \"type\": \"0\",\n" +
                   "            \"street\": null,\n" +
                   "            \"display_order\": 0,\n" +
                   "            \"deleted\": 0,\n" +
                   "            \"index_path\": \"0/\",\n" +
                   "            \"is_region\": \"true\",\n" +
                   "            \"identification\": null,\n" +
                   "            \"sub_type\": null,\n" +
                   "            \"longitude\": 132.6,\n" +
                   "            \"latitude\": 31.5,\n" +
                   "            \"related_org_index\": null,\n" +
                   "            \"related_org_name\": null,\n" +
                   "            \"owner_to\": null,\n" +
                   "            \"area_type\": null,\n" +
                   "            \"is_owner\": 1,\n" +
                   "            \"child\": [\n" +
                   "                {\n" +
                   "                    \"org_id\": 3,\n" +
                   "                    \"parent_id\": 2,\n" +
                   "                    \"org_index\": \"1000001001\",\n" +
                   "                    \"org_parent_index\": \"10\",\n" +
                   "                    \"org_name\": \"煤矿1\",\n" +
                   "                    \"region_code\": \"100000\",\n" +
                   "                    \"region_code_desc\": null,\n" +
                   "                    \"cascaded_id\": null,\n" +
                   "                    \"address\": null,\n" +
                   "                    \"description\": \"\",\n" +
                   "                    \"type\": \"0\",\n" +
                   "                    \"street\": \"\",\n" +
                   "                    \"display_order\": 0,\n" +
                   "                    \"deleted\": 0,\n" +
                   "                    \"index_path\": \"0/10/\",\n" +
                   "                    \"is_region\": \"false\",\n" +
                   "                    \"identification\": null,\n" +
                   "                    \"sub_type\": null,\n" +
                   "                    \"longitude\": null,\n" +
                   "                    \"latitude\": null,\n" +
                   "                    \"related_org_index\": null,\n" +
                   "                    \"related_org_name\": null,\n" +
                   "                    \"owner_to\": null,\n" +
                   "                    \"area_type\": null,\n" +
                   "                    \"is_owner\": 1,\n" +
                   "                    \"child\": null\n" +
                   "                }\n" +
                   "            ]\n" +
                   "        }\n" +
                   "    ]\n" +
                   "}";
           // 创建 ObjectMapper 实例
           ObjectMapper objectMapper = new ObjectMapper();

           try {
               // 将 JSON 字符串转换为 Map
               Map<String, Object> orgInfoMap = objectMapper.readValue(orgName, Map.class);
               ArrayList<Map<String,Object>> orgInfoList = new ArrayList<>();
               List<Map<String,Object>> data = (List<Map<String,Object>>)orgInfoMap.get("data");
               for (Map<String,Object> org: data){
                   HashMap<String, Object> map1 = new HashMap<>();

                   Integer org_id = (Integer)org.get("org_id");
                   String org_name = (String)org.get("org_name");
                   map1.put("org_id",org_id);
                   map1.put("org_name",org_name);
                   orgInfoList.add(map1);

                   List<Map<String,Object>> child = (List<Map<String,Object>>) org.get("child");
                   for (Map<String,Object> childOrg :child){

                       HashMap<String, Object> childMap = new HashMap<>();

                       Integer child_org_id = (Integer)childOrg.get("org_id");
                       String child_org_name = (String)childOrg.get("org_name");

                       childMap.put("org_id",child_org_id);
                       childMap.put("org_name",child_org_name);

                       orgInfoList.add(childMap);
                   }
               }

               int i1 = tRiskOrgidUnitcodeService.deleteTRiskOrgidUnitcodeAll();

               for (Map<String,Object> map :orgInfoList){
                   Integer org_id = (Integer)map.get("org_id");
                   String org_name = (String)map.get("org_name");
                   TRiskOrgidUnitcode tRiskOrgidUnitcode = new TRiskOrgidUnitcode();
                   tRiskOrgidUnitcode.setOrgId(org_id.toString());
                   tRiskOrgidUnitcode.setOrgName(org_name);
                   int i = tRiskOrgidUnitcodeService.insertTRiskOrgidUnitcode(tRiskOrgidUnitcode);
               }

           } catch (Exception e) {
               e.printStackTrace();
           }
       }

    }

    {
          String orgName ="{\n" +
                "    \"error_code\": \"0000000000\",\n" +
                "    \"message\": \"操作成功\",\n" +
                "    \"data\": [\n" +
                "        {\n" +
                "            \"org_id\": 2,\n" +
                "            \"parent_id\": 0,\n" +
                "            \"org_index\": \"10\",\n" +
                "            \"org_parent_index\": \"0\",\n" +
                "            \"org_name\": \"本域\",\n" +
                "            \"region_code\": \"100000\",\n" +
                "            \"region_code_desc\": null,\n" +
                "            \"cascaded_id\": null,\n" +
                "            \"address\": null,\n" +
                "            \"description\": null,\n" +
                "            \"type\": \"0\",\n" +
                "            \"street\": null,\n" +
                "            \"display_order\": 0,\n" +
                "            \"deleted\": 0,\n" +
                "            \"index_path\": \"0/\",\n" +
                "            \"is_region\": \"true\",\n" +
                "            \"identification\": null,\n" +
                "            \"sub_type\": null,\n" +
                "            \"longitude\": 132.6,\n" +
                "            \"latitude\": 31.5,\n" +
                "            \"related_org_index\": null,\n" +
                "            \"related_org_name\": null,\n" +
                "            \"owner_to\": null,\n" +
                "            \"area_type\": null,\n" +
                "            \"is_owner\": 1,\n" +
                "            \"child\": [\n" +
                "                {\n" +
                "                    \"org_id\": 3,\n" +
                "                    \"parent_id\": 2,\n" +
                "                    \"org_index\": \"1000001001\",\n" +
                "                    \"org_parent_index\": \"10\",\n" +
                "                    \"org_name\": \"煤矿1\",\n" +
                "                    \"region_code\": \"100000\",\n" +
                "                    \"region_code_desc\": null,\n" +
                "                    \"cascaded_id\": null,\n" +
                "                    \"address\": null,\n" +
                "                    \"description\": \"\",\n" +
                "                    \"type\": \"0\",\n" +
                "                    \"street\": \"\",\n" +
                "                    \"display_order\": 0,\n" +
                "                    \"deleted\": 0,\n" +
                "                    \"index_path\": \"0/10/\",\n" +
                "                    \"is_region\": \"false\",\n" +
                "                    \"identification\": null,\n" +
                "                    \"sub_type\": null,\n" +
                "                    \"longitude\": null,\n" +
                "                    \"latitude\": null,\n" +
                "                    \"related_org_index\": null,\n" +
                "                    \"related_org_name\": null,\n" +
                "                    \"owner_to\": null,\n" +
                "                    \"area_type\": null,\n" +
                "                    \"is_owner\": 1,\n" +
                "                    \"child\": null\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";
    }

}


